package adminLoginPack;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import ImmovableIndia.ConnectionPool;

public class DefinitionsAction extends Action {

	  ConnectionPool pool=null;
	  public ActionForward execute(ActionMapping mapping, ActionForm inForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
		  ResultSet rs = null;
		  Connection con=null;
		  Statement st = null;
		 
		  try{
			  	String methodName = request.getParameter("methodName");
			  
			  	String circularNumbers = "";
			  	List optionsList123=new ArrayList();
			  	
			  	
			  	int isUpdate = 0;
			  	con=getConnection();
				st=con.createStatement();
				
				ArrayList circularNumbersBasedOnServiceNumber=new ArrayList();

				if("gettingcircularNumebrsbasedOnServiceNumber".equals(methodName)){
					String seviceNumber = request.getParameter("selectedServiceOption");
					rs=st.executeQuery("select serialnumber,act_rule from definitions where service_no="+seviceNumber);
					while(rs.next()){
						circularNumbersBasedOnServiceNumber.add(rs.getString(1)+"~~"+rs.getString(2));
					}
					request.setAttribute("circularNumbersBasedOnServiceNumber", circularNumbersBasedOnServiceNumber);
					request.setAttribute("ServiveNumbersData", "ServiveNumbersData");
					response.getWriter().write(circularNumbers);				
					
				}else if("circularNumberDetailsBasedOnServicenumberAndCircularDate".equals(methodName)){
					
					String selectedServiceNumber = request.getParameter("selectedServiceNumber");
					String selectedCircularNumber = request.getParameter("selectedCircularNumber");
					String[] circularnumAndDate = selectedCircularNumber.split("~~");
					
					rs=st.executeQuery("select act_rule,definition from definitions where service_no="+selectedServiceNumber+" and serialnumber='"+circularnumAndDate[0]+"' and act_rule='"+circularnumAndDate[1].trim()+"'");
					while(rs.next()){
						request.setAttribute("act_rule", rs.getString(1));
						request.setAttribute("definition", rs.getString(2));
						
					}
					//request.setAttribute("XXX", "ServiveNumbersData");
					request.setAttribute("ServiveNumbersData", "circularNumberDetailsBasedOnServicenumberAndCircularDate");
					response.getWriter().write(circularNumbers);
					
					
					
				}else if("circularNumberDetails".equals(methodName)){
					
					String selectedCircularNumber = request.getParameter("selectedCircularNumber");
					String selectedServiceNumber = request.getParameter("selectedServiceNumber");
					
					rs=st.executeQuery("select Date_of_circular,circular_subject,master from circulars where service_no="+selectedServiceNumber+" and TOPIC='"+selectedCircularNumber+"'");
					
				
					String arr[] = new String[15];
					int i = 0;
					while(rs.next()){
						
						String str = rs.getString(1);
						arr[i]=str;
						i++;
				}
					request.setAttribute("arr", arr);
					request.setAttribute("circularNumbersBasedOnServiceNumber", circularNumbersBasedOnServiceNumber);
					request.setAttribute("ServiveNumbersData", "CircularNumbersData");
					
					response.getWriter().write(circularNumbers);

				}
				
				
		  }catch(Exception e){
			  e.printStackTrace();
		  }finally{
			  try {
					if(null != rs)
						pool.releaseResources(rs);
					System.out.println("Connection RS closed........--------------");
					if(null != st)
						pool.releaseResources(st);
					System.out.println("Connection ST closed........--------------");
					if(null != con)
					pool.releaseConnection(con);
					System.out.println("Connection con closed........--------------");
				} catch (Exception e) {
					e.printStackTrace();
				}
		  }
		  return mapping.findForward("circularnumbers");
	  }
	  public Connection getConnection() throws SQLException, Exception {
			pool=new ConnectionPool();
			return pool.getConnection();
	}
}
